#include "MainMenuScene.h"
#include "GameScene.h"
#include "AppDelegate.h"
#include "CStringRes.h"
#include "GameGlobal.h"
#include "ScoreScene.h"
#include "OptionScene.h"
#include "HelpScene.h"

using namespace cocos2d;

#define LINE_SPACE	40
#define MENU_POS_Y	430
#define MENU_BTN_HEIGHT	71

CCScene* MainMenuScene::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        MainMenuScene *layer = MainMenuScene::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MainMenuScene::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////
		
		/*
        // 1. Add a menu item with "X" image, which is clicked to quit the program.
		
        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);
		*/

		// Get window size
        CCSize size = CCDirector::sharedDirector()->getWinSize();

		// string table test
		CStringRes *strRes = AppDelegate::sharedStringRes();

		// add menu

		// new game
		CCMenuItemImage *pNewItem = CCMenuItemImage::create("Images/btn-new-normal.png", "Images/btn-new-selected.png",
																		this, menu_selector(MainMenuScene::menuCallback));
		pNewItem->setTag(MENU_TAG_NEW);
		pNewItem->setPosition(ccp(size.width/2, MENU_POS_Y));
		// continue
		CCMenuItemImage *pContinueItem = CCMenuItemImage::create("Images/btn-continue-normal.png", "Images/btn-continue-selected.png",
																		this, menu_selector(MainMenuScene::menuCallback));
		pContinueItem->setTag(MENU_TAG_CONTINUE);
		pContinueItem->setPosition(ccp(size.width/2, MENU_POS_Y - MENU_BTN_HEIGHT));
		// high scores
		CCMenuItemImage *pScoreItem = CCMenuItemImage::create("Images/btn-score-normal.png", "Images/btn-score-selected.png",
																		this, menu_selector(MainMenuScene::menuCallback));
		pScoreItem->setTag(MENU_TAG_SCORE);
		pScoreItem->setPosition(ccp(size.width/2, MENU_POS_Y - MENU_BTN_HEIGHT*2));
		// option
		CCMenuItemImage *pOptionItem = CCMenuItemImage::create("Images/btn-option-normal.png", "Images/btn-option-selected.png",
																		this, menu_selector(MainMenuScene::menuCallback));
		pOptionItem->setTag(MENU_TAG_OPTION);
		pOptionItem->setPosition(ccp(size.width/2, MENU_POS_Y - MENU_BTN_HEIGHT*3));
		// help
		CCMenuItemImage *pHelpItem = CCMenuItemImage::create("Images/btn-help-normal.png", "Images/btn-help-selected.png",
																		this, menu_selector(MainMenuScene::menuCallback));
		pHelpItem->setTag(MENU_TAG_HELP);
		pHelpItem->setPosition(ccp(size.width/2, MENU_POS_Y - MENU_BTN_HEIGHT*4));

        CCMenu* pMenu = CCMenu::create(pNewItem, pContinueItem, pScoreItem, pOptionItem, pHelpItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to ManiMenuScene layer as a child layer.
        this->addChild(pMenu, 1);

        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::create("Images/mainmenu_bg.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);

        bRet = true;
    } while (0);

    return bRet;
}

void MainMenuScene::menuCallback(CCObject* pSender)
{
    // "close" menu item clicked
	CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);

	switch (pMenuItem->getTag())
	{
		case MENU_TAG_NEW :
			{
				GameGlobal::init(GAME_GLOBAL_GAMEDATA);

				GameScene *pScene = GameScene::create();
				CCDirector::sharedDirector()->replaceScene(pScene);
			}
			break;

		case MENU_TAG_CONTINUE :
			{
				// to load the save data
				AppDelegate::loadGameData();
				// Game Scene
				GameScene *pScene = GameScene::create();
				CCDirector::sharedDirector()->replaceScene(pScene);
			}
			break;

		case MENU_TAG_SCORE :
			{
				CCScene *pScene = ScoreScene::scene();
				CCDirector::sharedDirector()->pushScene(pScene);
			}
			break;

		case MENU_TAG_OPTION :
			{
				CCScene *pScene = OptionScene::scene();
				CCDirector::sharedDirector()->pushScene(pScene);
			}
			break;

		case MENU_TAG_HELP :
			{
				CCScene *pScene = HelpScene::scene();
				CCDirector::sharedDirector()->pushScene(pScene);
			}
			break;
	}
}

